package com.collect.cameroon.zhiScene.zhiAuditer.zhiRoot;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.cameroon.R;
import com.collect.cameroon.zhiBase.BaseActivity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCustomerRepayOrderListEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISDailyCaseEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiAccount.ZHIAuditerAccountFragment;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiFollow.ZHIAuditerFollowFragment;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiFollow.zhiD0.ZHIAuditerD0Fragment;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiPerformance.ZHIAuditerPerfermanceFragment;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterCustomerService.ZHIFilterCustomerServiceAuditerD0;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDaily;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.cameroon.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_AUDITER)
public class ZHIRootAuditerActivity extends BaseActivity implements ZHIRootAuditerFragmentInterface {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_daily)
    ZHIFilterDaily drawer_filter_daily;

    @BindView(R.id.drawer_filter_customer_service)
    ZHIFilterCustomerServiceAuditerD0 drawer_filter_customer_service;

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;
    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;
    @BindView(R.id.bar_perfermance)
    ZHIBottomTabBarItem bar_perfermance;

    private FragmentManager mFragmentManager;
    private ZHIAuditerFollowFragment mZHIAuditerFollowFragment;
    private ZHIAuditerPerfermanceFragment mZHIAuditerPerfermanceFragment;
    private ZHIAuditerAccountFragment mZHIAuditerAccountFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_aduiter;
    }

    @Override
    protected void initInjector() {
    }

    @Override
    protected void initView() {
        try {
            mFragmentManager = getSupportFragmentManager();

            drawer_filter_daily.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
                @Override
                public void onConfirmClick() {
                    try {
                        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                            drawer_layout.closeDrawer(drawer_filter_daily);
                        }

                        if (mZHIAuditerFollowFragment != null) {
                            mZHIAuditerFollowFragment.refreshData();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                        drawer_layout.closeDrawer(drawer_filter_daily);
                    }
                }
            });
            drawer_filter_daily.setDefaultMember(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserName());
            drawer_filter_daily.setDisableMemberSelect(!ZHIMainProcessHelper.getInstance().getLoginEntity().isCustomerFlag());


            drawer_filter_customer_service.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
                @Override
                public void onConfirmClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_customer_service)) {
                        drawer_layout.closeDrawer(drawer_filter_customer_service);
                    }
                    if (ZHIAuditerD0Fragment.getInstance() != null) {
                        ZHIAuditerD0Fragment.getInstance().refreshData();
                    }
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_customer_service)) {
                        drawer_layout.closeDrawer(drawer_filter_customer_service);
                    }
                }
            });

            initEvent();
            selecFragment(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onReloadGroupUserList() {
//        if (drawer_filter_performance != null) {
//            drawer_filter_performance.updateUserDataSource();
//        }
    }

    @Override
    public void onReloadDailyListForPartRepayFlag() {
        try {
            if (drawer_filter_daily != null) {
                drawer_filter_daily.setPartRepay("YES");
            }
            selecFragment(0);
            if (mZHIAuditerFollowFragment != null) {
                mZHIAuditerFollowFragment.refreshData();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onShowFilterDaily() {
        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
            drawer_layout.closeDrawer(drawer_filter_daily);
        } else {
            drawer_layout.openDrawer(drawer_filter_daily);
        }
    }

    @Override
    public void onShowFilterPerformanceAmount() {

    }

    @Override
    public void onShowFilterPerformanceMember() {

    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterMemberParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterAmountParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterOrderParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISDailyCaseEntity onGetDailyFilterParams() {
        ZHISDailyCaseEntity zhisDailyCaseEntity = new ZHISDailyCaseEntity();
        zhisDailyCaseEntity.setTypeOne(drawer_filter_daily.getOrderType());
        zhisDailyCaseEntity.setTypeTwo(drawer_filter_daily.getCallType());
        zhisDailyCaseEntity.setContactResult(drawer_filter_daily.getCallResult());
        zhisDailyCaseEntity.setPtpTimeStart(drawer_filter_daily.getPromisedRepayStartDate());
        zhisDailyCaseEntity.setPtpTimeEnd(drawer_filter_daily.getPromisedRepayEndDate());
        zhisDailyCaseEntity.setRepayStartTime(drawer_filter_daily.getDudStartDate());
        zhisDailyCaseEntity.setRepayEndTime(drawer_filter_daily.getDueEndDate());
        zhisDailyCaseEntity.setCalcType(drawer_filter_daily.getCompareFlag());
        zhisDailyCaseEntity.setOverdueDay(drawer_filter_daily.getCompareOverdueDays());
        zhisDailyCaseEntity.setMobile(drawer_filter_daily.getUserMobile());
        zhisDailyCaseEntity.setPartRepayFlag(drawer_filter_daily.getPartRepay());

        //如果不选择成员，不用赋值，使用默认
        if (!TextUtils.isEmpty(drawer_filter_daily.getMember())) {
            zhisDailyCaseEntity.setSysLoginName(drawer_filter_daily.getMember());
        }

        return zhisDailyCaseEntity;
    }

    ///////// D0

    @Override
    public void onShowFilterFollow() {
        if (drawer_layout.isDrawerOpen(drawer_filter_customer_service)) {
            drawer_layout.closeDrawer(drawer_filter_customer_service);
        } else {
            drawer_layout.openDrawer(drawer_filter_customer_service);
        }
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterParams() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service.getOrderType());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service.getPaidStatus());
        return zhisCustomerRepayOrderListEntity;
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterD0Params() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service.getOrderTypeD0());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service.getPaidStatus());
        return zhisCustomerRepayOrderListEntity;
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterD1Params() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service.getOrderTypeD1());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service.getPaidStatus());
        return zhisCustomerRepayOrderListEntity;
    }


    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_perfermance.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_perfermance.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(1);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_perfermance.setChecked(false);
            selecFragment(2);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_perfermance.setChecked(false);
                bar_account.setChecked(false);
                if (null == mZHIAuditerFollowFragment) {
                    mZHIAuditerFollowFragment = new ZHIAuditerFollowFragment();
                    fragemntTransaction.add(R.id.content, mZHIAuditerFollowFragment);
                } else {
                    fragemntTransaction.show(mZHIAuditerFollowFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_perfermance.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHIAuditerPerfermanceFragment) {
                    mZHIAuditerPerfermanceFragment = new ZHIAuditerPerfermanceFragment();
                    fragemntTransaction.add(R.id.content, mZHIAuditerPerfermanceFragment);
                } else {
                    fragemntTransaction.show(mZHIAuditerPerfermanceFragment);
                }
                break;
            }
            case 2: {
                bar_loan.setChecked(false);
                bar_perfermance.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHIAuditerAccountFragment) {
                    mZHIAuditerAccountFragment = new ZHIAuditerAccountFragment();
                    fragemntTransaction.add(R.id.content, mZHIAuditerAccountFragment);
                } else {
                    fragemntTransaction.show(mZHIAuditerAccountFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;

        if (null != mZHIAuditerFollowFragment) {
            fragemntTransaction.hide(mZHIAuditerFollowFragment);
        }
        if (null != mZHIAuditerAccountFragment) {
            fragemntTransaction.hide(mZHIAuditerAccountFragment);
        }

        if (null != mZHIAuditerPerfermanceFragment) {
            fragemntTransaction.hide(mZHIAuditerPerfermanceFragment);
        }
    }
}
